-
Notifications
You must be signed in to change notification settings - Fork 29.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
src: do proper StringBytes error handling #12765
Conversation
lib/buffer.js
Outdated
@@ -590,11 +589,8 @@ Buffer.prototype.toString = function(encoding, start, end) { | |||
|
|||
if (end <= start) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is much clearer now, had to double check undefined <= undefined
returns false due to number hint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benjamingr Not sure, what are you saying? 😄 I didn’t really touch this part of the logic ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You added an early return
which eliminates a correct (but very tricky) behavior in the old code where it would check if end <= start
for undefined values. I'm just saying I'm happy with the change :)
src/node_crypto.cc
Outdated
delete[] md_value; | ||
args.GetReturnValue().Set(rc); | ||
// TODO(addaleax): Throw `error` here. This isn't so terribly important, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So why not throw
the error here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, right, I could just do that here. I didn’t want to accidentally turn this into a semver-major change, but I guess here it really doesn’t make a difference… Updated!
MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, | ||
const uint16_t* buf, | ||
size_t buflen, | ||
Local<Value>* error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I would use a reference and not a pointer here - passing Isolate as a pointer makes sense since it's convention - but if the goal is to literally a const pointer to an object that might change (but not the pointer) I think a reference is cleaner. If this is convention and we can't do a reference - can we make it a const pointer?
(goes to all sites)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I agree. But yes, we don’t use non-const references in this codebase, and I can live with that… ¯\_(ツ)_/¯
If this is convention - can we make it a const pointer?
Sorry, which thing should be made const? The Local
can’t be const, Encode()
needs to be able to assign to it. The pointer itself could be const, but it’s a pass-by-value function parameter, so that doesn’t really matter anyway, does it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pointer itself, so it's clear it doesn't change - but I don't feel strongly about this and it's plenty obvious it won't change (like the isolate).
src/string_bytes.cc
Outdated
|
||
case BASE64: { | ||
size_t dlen = base64_encoded_size(buflen); | ||
char* dst = node::UncheckedMalloc(dlen); | ||
if (dst == nullptr) { | ||
return Local<String>(); | ||
goto malloc_failed; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a function containing the logic of malloc_failed
and call sites calling the functions (rather than using goto
is cleaner. I don't have a lot of experience with this code though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve just inlined the malloc_failed
case, it was a lot bulkier and made more sense this way when I started editing. 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have several nits:
Lots of new TODOs that can probably be fixed in this PR pretty quickly.I think a reference is cleaner (or at least a const pointer)Not agoto
fan.
That said, I gave the code a read, understand what it does and the changes look good to me with or without the nits addressed - given we can always address them later nits addressed.
I'd feel more comfortable if someone more experienced with the code gave this a review too though :)
I’d like to keep them for a quick semver-major follow-up PR after this lands, if that’s okay. |
@addaleax sounds good to me. Editing previous comment. |
lib/buffer.js
Outdated
if (arguments.length === 0) { | ||
result = this.utf8Slice(0, this.length); | ||
return this.utf8Slice(0, this.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By changing this to return
, you can drop the else
and save a level of indentation on the rest of the code below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cjihrig yup, done!
- Return `MaybeLocal`s from `StringBytes::Encode` - Add an `error` out parameter to pass JS exceptions to the callers (instead of directly throwing) - Simplify some of the string generation methods in `string_bytes.cc` by unifying the `EXTERN_APEX` logic - Reduce usage of deprecated V8 APIs. - Remove error handling logic from JS, the `buffer.*Slice()` methods now throw errors themselves. - Left TODO comments for future semver-major error message improvements. This paves the way for better error messages coming out of the StringBytes methods. Ref: nodejs#3175
dc00cac
to
389af5a
Compare
Nothing to add, looks good :) |
Landed in d56a7e6 |
- Return `MaybeLocal`s from `StringBytes::Encode` - Add an `error` out parameter to pass JS exceptions to the callers (instead of directly throwing) - Simplify some of the string generation methods in `string_bytes.cc` by unifying the `EXTERN_APEX` logic - Reduce usage of deprecated V8 APIs. - Remove error handling logic from JS, the `buffer.*Slice()` methods now throw errors themselves. - Left TODO comments for future semver-major error message improvements. This paves the way for better error messages coming out of the StringBytes methods. Ref: #3175 PR-URL: #12765 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
- Return `MaybeLocal`s from `StringBytes::Encode` - Add an `error` out parameter to pass JS exceptions to the callers (instead of directly throwing) - Simplify some of the string generation methods in `string_bytes.cc` by unifying the `EXTERN_APEX` logic - Reduce usage of deprecated V8 APIs. - Remove error handling logic from JS, the `buffer.*Slice()` methods now throw errors themselves. - Left TODO comments for future semver-major error message improvements. This paves the way for better error messages coming out of the StringBytes methods. Ref: nodejs#3175 PR-URL: nodejs#12765 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
Should this be backported to |
MaybeLocal
s fromStringBytes::Encode
error
out parameter to pass JS exceptions to the callers (instead of directly throwing)string_bytes.cc
by unifying theEXTERN_APEX
logicbuffer.*Slice()
methods now throw errors themselves.This paves the way for better error messages coming out of the StringBytes methods.
Ref: #3175
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
buffer, fs, os, crypto
CI: https://ci.nodejs.org/job/node-test-commit/9528/
Maybe @TimothyGu or @tniessen would want to review?
Also, /cc @MylesBorins , if you’re still interested in #3175 … that should become a lot easier after this :)